<div class="row">
    <nav class="navbar navbar-default navbar-static-top">
        <div class="page-title">申请表单:请填写接入平台的必要信息</div>
    </nav>
    <div class="show-content">
        <form>
            <div class="form-horizontal">
                <div class="form-group">
                    <input type="hidden" id="id" value="{{$game->id}}">
                    <label class="col-lg-2 input-title-right"><i>*</i> 游戏类别</label>
                    <label class="col-lg-8">
                        <fieldset disabled>
                            <select id="class" name="class" class="form-control">
                                @if($game->class == 0)
                                    <option value=0>网页免费版</option>
                                @elseif($game->class == 1)
                                    <option value=1>网页收费版</option>
                                @elseif($game->class == 2)
                                    <option value=2>安卓免费版</option>
                                @else
                                    <option value=3>安卓收费版</option>
                                @endif
                            </select>
                        </fieldset>
                    </label>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>* </i> 产品名称</label>
                    <label class="col-lg-8">
                        <input type="text" class="form-control" id="name" name="name" placeholder="产品名称" value="{{$game->name}}">
                    </label>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i> 产品英文名称</label>
                    <label class="col-lg-8">
                        <input type="text" class="form-control" id="en_name" name="name_en" placeholder="产品英文名称"
                               value="{{$game->name_en}}">
                    </label>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right">游戏类型</label>
                    <label class="col-lg-8">
                        <select name="type" id="type"  class="form-control">
                            <option @if($game->class == 1)   selected="selected" @endif value=1>角色扮演</option>
                            <option @if($game->class == 2)   selected="selected" @endif value=2>桌面游戏</option>
                            <option @if($game->class == 3)   selected="selected" @endif value=3>射击游戏</option>
                            <option @if($game->class == 4)   selected="selected" @endif value=4>运动游戏</option>
                            <option @if($game->class == 5)   selected="selected" @endif value=5>动作游戏</option>
                            <option @if($game->class == 6)   selected="selected" @endif value=6>卡片游戏</option>
                            <option @if($game->class == 7)   selected="selected" @endif value=7>冒险游戏</option>
                            <option @if($game->class == 8)   selected="selected" @endif value=8>竞速游戏</option>
                            <option @if($game->class == 9)   selected="selected" @endif value=9>益智游戏</option>
                            <option  @if($game->class == 10) selected="selected" @endif value=10>策略游戏</option>
                            <option  @if($game->class == 11) selected="selected" @endif value=11>音乐游戏</option>
                            <option  @if($game->class == 12) selected="selected" @endif value=12>网页游戏</option>
                        </select>
                    </label>
                </div>
                @if($game->class == 1 || $game->class == 3)
                    <div class="form-group">
                        <label class="col-lg-2 input-title-right"><i>*</i> 产品公钥</label>
                        <div class="col-lg-8">
                            <input type="hidden" id="public_key" name="public_key" value="{{$ext->public_key}}">
                            <input type="file" id="key-file" style="width: 0;height: 0">
                            <button type="button" class="btn btn-primary btn-sm" onclick="$('#key-file').click()" >
                                点击 上传
                            </button>
                            <span class="msg">@if($ext->public_key) <label style="color: #2ab27b">已上传KEY</label> @endif</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-2 input-title-right"><i>*</i> 支付回掉地址</label>
                        <label class="col-lg-8">
                            <input type="text" class="form-control" id="notify_url" name="notify_url" placeholder="平台服务器回掉GAME SERVER URL   http://you host... "
                                   value="{{$ext->notify_url}}">
                        </label>
                    </div>
                @endif
            </div>

            <div class="form-group row-jf">
                <button type="submit" class="btn btn-success"> 提交</button>
                <button type="button" class="btn btn-danger" onclick="getData('{{url('/apply_ist')}}')"> 取消</button>
            </div>
        </form>
    </div>
</div>
<script>
    var classVal = {{$game->class}}
    //密钥处理
    $('#key-file').change(function () {
        var files = this.files;
        var dom   = $(this);
        if (files.length) {
            var file = files[0];
            var reader = new FileReader();//new一个FileReader实例
            if (file.type === '') {//判断文件类型 当前无类型
                reader.onload = function() {
                    $('#public_key').val(reader.result)
                    dom.attr('disabled','disabled');
                    var html = '<span style="color: forestgreen;line-height: inherit;padding-left: 20px">上传成功</span>';
                    dom.parent().find('.msg').html(html);
                    return   false;
                };
                reader.readAsText(file);
            }
            var html = '<span style="color: red;line-height: inherit;padding-left: 20px">上传失败！请重试</span>';
            dom.parent().find('.msg').html(html);
        }
    })
    var validation = {
        name: {
            validators: {
                notEmpty: {
                    message: '名称不能为空'
                },
                stringLength: {
                    min: 2,
                    max: 8,
                    message: '名称长度必须在2到8位之间'
                },
            }
        },
        name_en: {
            validators: {
                notEmpty: {
                    message: '英文名称不能为空'
                },
                stringLength: {
                    min: 2,
                    max: 20,
                    message: '英文名称长度必须在2到20位之间'
                },
            }
        },
        type:{
            validators: {
                notEmpty: {
                    message: '游戏类型必须选择一项'
                }
            }
        }
    };

    if(classVal == 1 || classVal == 3) { //付费
        validation['public_key']= {
            validators: {
                notEmpty: {
                    message: '请上传KEY'
                }
            }
        };
        validation['notify_url']= {
            validators: {
                notEmpty: {
                    message: '支付回掉地址不能为空'
                }
            }
        };
    }

    $(function () {
        $('form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields:validation
        }).on("success.form.bv", function (e) {
            layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
            let data = {
                id:$('#id').val(),
                name: $('#name').val(),
                name_en: $('#en_name').val(),
                class: $('#class').val(),
                type:$('#type').val(),
                public_key:$('#public_key').val(),
                notify_url:$('#notify_url').val(),
                _token: $('#_token').val()

            };
            $.ajax(
                {
                    method: 'post',
                    url: '{{URL::asset('/save_product')}}',
                    async: true,
                    dataType: 'text',
                    data: data,
                    success: function (data) {
                        layer.closeAll();
                        $('#page-content-wrapper').html(data)
                    },
                    error: function (err) {
                        console.log(err);
                        layer.closeAll();
                        layer.msg('错误!请重试!', {icon: 5, time: 1500})
                    }
                });

            return false;//阻止表单跳转

        });

    });

</script>
